﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using DataObjectsPhone;
using System.Device.Location;
using Microsoft.Maps.MapControl;
namespace ee_wp_tourmaker.Views
{
    public partial class NewCheckpointPivot : PhoneApplicationPage
    {

        GeoCoordinateWatcher myCoordinateWatcher;
        double lati = 0.0;
        double longi = 0.0;
        
        public NewCheckpointPivot()
        {
            InitializeComponent();

            ListPickerItem i1 = new ListPickerItem();
            i1.Content = "Up to 50 meters";
            ListPickerItem i2 = new ListPickerItem();
            i2.Content = "Up to 1km";
            ListPickerItem i3 = new ListPickerItem();
            i3.Content = "Up to 5km";
            List<ListPickerItem> collection = new List<ListPickerItem>();
            collection.Add(i1);
            collection.Add(i2);
            collection.Add(i3);
            accuracyListPicker.ItemsSource = collection;
            //accuracyListPicker.Items.Add(i1);
            //accuracyListPicker.Items.Add(i2);
            //accuracyListPicker.Items.Add(i3);

        }

        private void ApplicationBarSaveButton_Click(object sender, EventArgs e)
        {
            CheckPoint checkpoint = new CheckPoint();
            checkpoint.Name = nameTextBox.Text;
            checkpoint.Description = descTextBox.Text;
            checkpoint.Latitude = (Decimal)lati;
            checkpoint.Longitude = (Decimal)longi;
            switch (accuracyListPicker.SelectedIndex)
            {
                case 0:
                    checkpoint.Accuracy = 50;
                    break;
                case 1:
                    checkpoint.Accuracy = 1000;
                    break;
                case 2:
                    checkpoint.Accuracy = 5000;
                    break;
            }

            if (checkpoint.Name.Length != 0 && checkpoint.Latitude != (Decimal) 0.0)
            {
                App.MainModel.newChallenge.checkpointsList.Add(checkpoint);
                NavigationService.GoBack();
            }
            else
            {
                MessageBox.Show("You must select a point on the map and a name for your checkpoint!");
            }
        }

        private void ApplicationBarCancelButton_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        private void BingMap_Loaded(object sender, RoutedEventArgs e)
        {
            checkPointMap.NavigationVisibility = System.Windows.Visibility.Collapsed;
            double lati1 = 59.4364700317383;
            double longi2 = 24.7525997161865;

            checkPointMap.Center = new Location(lati1, longi2);
            checkPointMap.ZoomLevel = 12;
            myCoordinateWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
            //myCoordinateWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(myCoordinateWatcher_PositionChanged);
        }
        /*
        void myCoordinateWatcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            if (!e.Position.Location.IsUnknown)
            {
                lati = e.Position.Location.Latitude;
                longi = e.Position.Location.Longitude;
                checkPointMap.Center = new Location(lati1, longi2);
            }
        }*/

        private void checkPointMap_MouseClick(object sender, MapMouseEventArgs e)
        {
            Location p = checkPointMap.ViewportPointToLocation(e.ViewportPoint);

            lati = p.Latitude;
            longi = p.Longitude;

            longitudeTextBlock.Text = "Longitude: " + longi;
            latitudeTextBlock.Text = "Latitude: " + lati;

            checkPointMap.Children.Clear();
            Pushpin pin1 = new Pushpin();
            pin1.Location = p;
            checkPointMap.Children.Add(pin1);
        }

        private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (((Pivot)sender).SelectedIndex)
            {
                case 0:
                    ApplicationBar.IsVisible = true;
                    break;

                case 1:
                    ApplicationBar.IsVisible = false;
                    break;
            }
        }

        

    }
}